#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "struc_arg.h"
#include "process.h"

int checkArguments(int, char**, arg_t*);
int checkNames(char*, arg_t*);
int checkOptions(char*, arg_t*);


/*****************************************************/
 int main(int argc, char** argv)
{
    if (1 == argc)
    {

        printf("\033[35m\033Usage: grep [OPTION]... MOTIF [FICHIER]...\n\033[31mTry 'grep --help' for more information\033[0m\n");

        return 0;
    }

    arg_t triggerStruct;
    memset(&triggerStruct, 0, sizeof(arg_t));

    triggerStruct.firstArg_ = malloc(sizeof(path_t));
    path_t* head = triggerStruct.firstArg_ ;

    checkArguments(argc, argv, &triggerStruct);

    triggerStruct.firstArg_  = head;

    startProcessing(&triggerStruct);

    free( triggerStruct.trigger_o );
    free( triggerStruct.trigger_label );
    free( triggerStruct.trigger_include );
    free( triggerStruct.trigger_exclude );
    free( triggerStruct.trigger_exclude_from );
    free( triggerStruct.trigger_exclude_dir );
    free( triggerStruct.firstArg_ );

    return 0;
}

/**FUNCTION*******************************************/
int checkArguments(int pArgc, char** pArgv, arg_t* pStruct)
{
    int it = 1;
    char* number;
    while( pArgc > it)
    {
        if( '-' == pArgv[it][0] )
        {

            if ('s' == pArgv[it][1] || strcmp("--no-messages", pArgv[it]) == 0)
            {
                pStruct->trigger_s = 1;
            }
            else if ('v' == pArgv[it][1] || strcmp("--invert-match", pArgv[it]) == 0)
            {
                pStruct->trigger_v = 1;
            }

            else if ('V' == pArgv[it][1] || strcmp("--version", pArgv[it]) == 0)
            {
                pStruct->trigger_V = 1;
            }
            else if (strcmp("--help", pArgv[it]) == 0)
            {
                pStruct->trigger_help = 1;
            }
            else if (strcmp("--nmap", pArgv[it]) == 0)
            {
                pStruct->trigger_nmap = 1;
            }
            else if ('m' == pArgv[it][1] || NULL != strstr("--max-count=", pArgv[it]) )
            {
                number = strchr(pArgv[it], '=');
                char* str = malloc( sizeof(pArgv[it]) * sizeof(char) - (sizeof(strlen("--include=")) * sizeof(char)) );
                strcpy(str, number+1 );
                pStruct->trigger_m = atoi(str);
            }
            else if ('b' == pArgv[it][1] || strcmp("--byte-offset", pArgv[it]) == 0)
            {
                pStruct->trigger_b = 1;
            }
            else if ('n' == pArgv[it][1] || strcmp("--line-number", pArgv[it]) == 0)
            {
                pStruct->trigger_n = 1;
            }
            else if ('H' == pArgv[it][1] || strcmp("--with-filename", pArgv[it]) == 0)
            {
                pStruct->trigger_H = 1;
            }
            else if ('h' == pArgv[it][1] || strcmp("--no-filename", pArgv[it]) == 0)
            {
                pStruct->trigger_h = 1;
            }
            else if (NULL != strstr("--label=", pArgv[it]))
            {
                number = strchr(pArgv[it], '=');
                char* str = malloc( sizeof(pArgv[it]) * sizeof(char) - (sizeof(strlen("--include=")) * sizeof(char)) );
                strcpy(str, number+1 );
                pStruct->trigger_label = str;
            }
            else if ('o' == pArgv[it][1] || strcmp("--only-matching", pArgv[it]) == 0)
            {
                pStruct->trigger_o = pArgv[++it];
            }
            else if ('q' == pArgv[it][1] || strcmp("--quiet", pArgv[it]) == 0 || strcmp("--silent", pArgv[it]) == 0)
            {
                pStruct->trigger_q = 1;
            }
            else if (strcmp("--binary-file=binary", pArgv[it]) == 0)
            {
                pStruct->trigger_binary = 1;
            }
            else if ('a' == pArgv[it][1] || strcmp("--text", pArgv[it]) == 0 || strcmp("--binary-file=text", pArgv[it]) == 0)
            {
                pStruct->trigger_a = 1;
            }
            else if ('I' == pArgv[it][1] || strcmp("--binary-file=without-match", pArgv[it]) == 0)
            {
                pStruct->trigger_I = 1;
            }
            else if ('d' == pArgv[it][1] || NULL != strstr("--directories=", pArgv[it]))
            {
                if ( NULL != strstr("recurse", pArgv[it]) )
                {
                    pStruct->trigger_R = 1;
                }
                else if (NULL != strstr("skip", pArgv[it]))
                {
                    pStruct->trigger_d_skip = 1;
                }
                else if (NULL != strstr("read", pArgv[it]))
                {
                     pStruct->trigger_d = 1;
                }
            }
            else if ('D' == pArgv[it][1] || NULL != strstr("--devices=", pArgv[it]))
            {
                if (NULL != strstr("skip", pArgv[it]))
                {
                    pStruct->trigger_D_skip = 1;
                }
                else if (NULL != strstr("read", pArgv[it]))
                {
                    pStruct->trigger_D = 1;
                }
            }
            else if ('R' == pArgv[it][1] || 'r' == pArgv[it][1] || strcmp("--recursive", pArgv[it]) == 0)
            {
                pStruct->trigger_R = 1;
            }
            else if (NULL != strstr("--include=", pArgv[it]))
            {
                number = strchr(pArgv[it], '=');
                char* str = malloc( sizeof(pArgv[it]) * sizeof(char) - (sizeof(strlen("--include=")) * sizeof(char)) );
                strcpy(str, number+1 );
                pStruct->trigger_include = str;
            }
            else if (NULL != strstr("--exclude=", pArgv[it]))
            {
                number = strchr(pArgv[it], '=');
                char* str = malloc( sizeof(pArgv[it]) * sizeof(char) - (sizeof(strlen("--exclude=")) * sizeof(char)) );
                strcpy(str, number+1 );
                pStruct->trigger_exclude = str;
            }
            else if (NULL != strstr("--exclude-from=", pArgv[it]))
            {
                number = strchr(pArgv[it], '=');
                char* str = malloc( sizeof(pArgv[it]) * sizeof(char) - (sizeof(strlen("--exclude-from=")) * sizeof(char)) );
                strcpy(str, number+1 );
                pStruct->trigger_exclude_from = str;
            }
            else if (NULL != strstr("--exclude-dir=", pArgv[it]))
            {
                number = strchr(pArgv[it], '=');
                char* str = malloc( sizeof(pArgv[it]) * sizeof(char) - (sizeof(strlen("--exclude-dir=")) * sizeof(char)) );
                strcpy(str, number+1 );
                pStruct->trigger_exclude_dir = str;
            }
            else if ('L' == pArgv[it][1] || strcmp("--files-without-match", pArgv[it]) == 0)
            {
                pStruct->trigger_L = 1;
            }
            else if ('l' == pArgv[it][1] || strcmp("--files-with-matches", pArgv[it]) == 0)
            {
                pStruct->trigger_l = 1;
            }
            else if ('c' == pArgv[it][1] || strcmp("--count", pArgv[it]) == 0)
            {
                pStruct->trigger_c = 1;
            }
            else if ('T' == pArgv[it][1] || strcmp("--initial-tab", pArgv[it]) == 0)
            {
                pStruct->trigger_T = 1;
            }
            else if ('Z' == pArgv[it][1] || strcmp("--null", pArgv[it]) == 0)
            {
                pStruct->trigger_Z = 1;
            }
            else if ('B' == pArgv[it][1] || NULL != strstr("--before-context=", pArgv[it]))
            {
                number = strchr(pArgv[it], '=');
                char* str = malloc( sizeof(pArgv[it]) * sizeof(char) - (sizeof(strlen("--before-context=")) * sizeof(char)) );
                strcpy(str, number+1 );
                pStruct->trigger_B = atoi(str);
            }
            else if ('A' == pArgv[it][1] || NULL != strstr("--after-context=", pArgv[it]))
            {
                number = strchr(pArgv[it], '=');
                char* str = malloc( sizeof(pArgv[it]) * sizeof(char) - (sizeof(strlen("--after-context=")) * sizeof(char)) );
                strcpy(str, number+1 );
                pStruct->trigger_A = atoi(str);
            }
            else if ('C' == pArgv[it][1] || NULL != strstr("--context=", pArgv[it]))
            {
                number = strchr(pArgv[it], '=');
                char* str = malloc( sizeof(pArgv[it]) * sizeof(char) - (sizeof(strlen("--context=")) * sizeof(char)) );
                strcpy(str, number+1 );
                pStruct->trigger_C = atoi(str);

                //strcpy(str, pArgv[it] + strlen("--context=") );

                //char x = pArgv[it][strlen(pArgv[it]-1)];
                //pStruct->trigger_C = atoi(&x);
            }
            else if (isdigit(pArgv[it][1]))
            {
                pStruct->trigger_C = atoi(pArgv[it]);
            }
            else if (NULL != strstr("--color[=never]", pArgv[it]) || NULL != strstr("--colour[=never]", pArgv[it]))
            {
                pStruct->trigger_color_never = 1;
            }
            else if (NULL !=  strstr("--color[=always]", pArgv[it]) || NULL !=  strstr("--colour[=always]", pArgv[it]))
            {
                pStruct->trigger_color_always = 1;
            }
            else if (NULL != strstr("--color[=auto]", pArgv[it]) || NULL != strstr("--colour[=auto]", pArgv[it]))
            {
                pStruct->trigger_color_auto = 1;
            }
            else if ('U' == pArgv[it][1] || strcmp("--binary", pArgv[it]) == 0)
            {
                pStruct->trigger_U = 1;
            }
            else if ('u' == pArgv[it][1] || strcmp("--unix-byte-offsets", pArgv[it]) == 0)
            {
                pStruct->trigger_u = 1;
            }
        }

        else if( '-' != pArgv[it][0] )
        {
            checkNames(pArgv[it], pStruct);
        }
        ++it;
    }
    return 0;
}

int checkNames(char* pName, arg_t* pStruct)
{
    if( '-' != pName[0])
    {
            while( pStruct->firstArg_->next_ )
            {
                pStruct->firstArg_ = pStruct->firstArg_->next_;
            }

            pStruct->firstArg_->paths_ = malloc(sizeof(char) * strlen(pName));
            memcpy(pStruct->firstArg_->paths_, pName, strlen(pName));

            if( !pStruct->firstArg_->next_ )
            {
                pStruct->firstArg_->next_ = malloc(sizeof(path_t));
            }
    }

    return 0;
}
